/*
 * @Author: silence
 * @Date: 2023-07-06 09:14:45
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-07-27 17:36:03
 * @Description: 底部选择时间sheet
 */
import 'package:ems_neutral_app/pages/Mine/Language/LanguageController.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:flutter/cupertino.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../utils/color/ColorUtils.dart';
import '../../../utils/router/ARouter.dart';

class BottomTimeSheetView extends StatefulWidget {
  /// 选择第几个，默认选中第一个
  final int? initialSelectedItem;
  final Widget? itemWidget;

  /// 默认时间（24小时制）
  final int initialHours;

  /// 默认分钟（60分钟制）
  final int initialMinute;

  /// 高度,默认230
  final double? height;

  /// item高度,默认48
  final double? itemHeight;

  /// 确定，返回时间、分钟
  final void Function(int hour, int minute)? tapSureSeletedFunc;

  /// 取消
  final void Function()? tapCancelFunc;

  const BottomTimeSheetView(
      {this.height,
      this.initialSelectedItem,
      this.itemWidget,
      this.itemHeight,
      this.tapCancelFunc,
      this.tapSureSeletedFunc,
      super.key,
      this.initialHours = 9,
      this.initialMinute = 0});

  @override
  State<BottomTimeSheetView> createState() => _BottomTimeSheetViewState();
}

class _BottomTimeSheetViewState extends State<BottomTimeSheetView> {
  late int _curHour, _curMinute;
  late double _itemHeight;
  late FixedExtentScrollController _hourScrollController,
      _minuteScrollController;

  LanguageController _languageController = LanguageController();
  bool _isPromptEnglish = true;

  @override
  void initState() {
    super.initState();
    _curHour = widget.initialHours;
    _curMinute = widget.initialMinute;

    _itemHeight = widget.itemHeight ?? 48.w;

    _hourScrollController = FixedExtentScrollController(initialItem: _curHour);
    _minuteScrollController =
        FixedExtentScrollController(initialItem: _curMinute);

    _isPromptEnglish = _languageController.isPromptEnglish();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          color: ColorUtils.color_bottom_sheet,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(12.w), topRight: Radius.circular(12.w))),
      child: Column(
        children: [
          /// 顶部操作按钮
          Container(
            padding: EdgeInsets.symmetric(horizontal: 20.w),
            child: handlerBtns(),
            height: 50.w,
          ),

          /// 中间可选的内容
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10.w),
            height: widget.height ?? 230.w,
            child: contentView(),
          )
        ],
      ),
    );
  }

  handlerBtns() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        SilButton(
          title: LangOf(context)?.common?.cancel,
          titleColor: ColorUtils.color_subtitle,
          fontSize: 16.sp,
          fontWeight: FontWeight.w500,
          onTapFunc: () => widget.tapCancelFunc ?? ARoute.pop(context),
        ),
        SilButton(
          title: LangOf(context)?.common?.confirm,
          titleColor: ColorUtils.color_theme,
          fontSize: 16.sp,
          fontWeight: FontWeight.w500,
          onTapFunc: () {
            if (widget.tapSureSeletedFunc != null) {
              widget.tapSureSeletedFunc!(_curHour, _curMinute);
            }
            ARoute.pop(context);
          },
        ),
      ],
    );
  }

  contentView() {
    double itemW = MediaQuery.of(context).size.width * 0.5 - 10.w;
    return Container(
      height: widget.height ?? 230.w,
      child: Row(
        children: [
          Container(width: itemW, child: hourCupertinoPicker()),
          Container(width: itemW, child: minuteCupertinoPicker())
        ],
      ),
    );
  }

  /// 选择小时
  hourCupertinoPicker() {
    return CupertinoPicker.builder(
        scrollController: _hourScrollController,
        childCount: 24,
        itemExtent: _itemHeight,
        onSelectedItemChanged: (index) {
          _curHour = index;
        },
        itemBuilder: (context, index) {
          String str = index < 10 ? '0$index时' : '$index时';
          if(_isPromptEnglish){
            str = index < 10 ? '0$index' : '$index';
          }

          return Container(
            alignment: Alignment.center,
            child: SilText(
              str,
              fontSize: 14.sp,
              fontWeight: FontWeight.w400,
              color: ColorUtils.color_font_main,
            ),
          );
        },
        selectionOverlay: selectionOverlayWidget());
  }

  /// 选择分钟
  minuteCupertinoPicker() {
    return CupertinoPicker.builder(
        scrollController: _minuteScrollController,
        childCount: 60,
        itemExtent: _itemHeight,
        onSelectedItemChanged: (index) {
          _curMinute = index;
        },
        itemBuilder: (context, index) {
          String str = index < 10 ? '0$index分' : '$index分';
          if(_isPromptEnglish){
             str = index < 10 ? '0$index' : '$index';
          }
          return Container(
            alignment: Alignment.center,
            child: SilText(
              str,
              fontSize: 14.sp,
              fontWeight: FontWeight.w400,
              color: ColorUtils.color_font_main,
            ),
          );
        },
        selectionOverlay: selectionOverlayWidget());
  }

  /// 覆盖在中间的组件
  Widget selectionOverlayWidget() {
    Widget promptWidget = Container(
      height: _itemHeight - 1.w,
    );

    // 分割线
    Widget dividerWidget = Container(
      color: Color.fromRGBO(255, 255, 255, 0.1),
      height: 0.5.w,
    );

    return Column(
      children: [dividerWidget, promptWidget, dividerWidget],
    );
  }
}
